<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\ZoneRequest;
use App\User;
use \DB;
use Auth;
//use Illuminate\Http\Request;
use Validator;
use App\Http\Requests\ActivityPostRequest;
use App\Models\Zone;
use Illuminate\Support\Facades\Request;

class ZoneController extends CommonController
{

    public function postChildren()
    {
        if (!Request::has('id')) {
            return AjaxCallbackMessage('参数异常', false);
        }
        $id = Request::input('id');
        $zone = DB::table('zone')->where('upid', $id)->where('status', 1)->get();

        return AjaxCallbackMessage($zone, true);
    }

    /**
     * 获取城市列表
     */
    public function getList()
    {
        $lists = Zone::with('zone')->orderBy('id', 'asc')->paginate(10);

        return view('zone.list')->with('lists', $lists)->with("actions", $this->actions);
    }

    /**
     *执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request::input();
        $lists = Zone::with('zone')->orderBy('id', 'asc');
        if (isset($data['name']) && $data['name'] != '') {
            $lists->where('name', $data['name']);
        }

        if (isset($data['alpha']) && $data['alpha'] != '') {
            $lists->orWhere('alpha', $data['alpha']);
        }

        $lists = $lists->paginate(10);

        return view('zone.list')->with('lists', $lists)->with("actions", $this->actions);
    }

    /**
     * 获取编辑页面
     *
     */
    public function getEdit(Request $request)
    {
        $data = $request::input();
        $list = Zone::with('zone')->where('id', $data['id'])->first();
        $pLists = Zone::where('level', 0)->orWhere('level', 1)->orderBy('id', 'asc')->get();

//        dd($list);exit;

        return view('zone.edit')->with('list', $list)->with('pLists', $pLists);
    }

    /**
     * 执行一个编辑
     */
    public function postEdit(ZoneRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType']);
        Zone::where('id', $data['id'])->update($data);

        return AjaxCallbackMessage('编辑成功', true, url('zone/list'));
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {
        $pLists = Zone::where('level', 0)->orWhere('level', 1)->orderBy('id', 'asc')->get();

        return view('zone.create')->with('pLists', $pLists);
    }

    /**
     * 执行创建
     */
    public function postCreate(ZoneRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType']);
        $result = Zone::create($data);
        if (is_object($result)) {
            return AjaxCallbackMessage('创建成功', true, url('zone/list'));
        } else {
            return AjaxCallbackMessage('创建失败，未知错误', false);
        }
    }
}